const STORAGE_KEY = "LT_SESSION";
class Session {
    session = {};
    constructor(){
        this.restore();
    }
    restore(){
        let strJson = localStorage.getItem(STORAGE_KEY);
        if(strJson){
            this.session = JSON.parse(strJson);
        }
    }
    save(){
        let strJson = JSON.stringify(this.session);
        localStorage.setItem(STORAGE_KEY,strJson);
    }
    setToken(token){
        this.session.token = token;
        this.save();
    }
    getToken(){
        return this.session.token || "";
    }
    setItem(key,value){
        this.session[key] = value;
        this.save();
    }
    getItem(key){
        return this.session[key];
    }
    addKeyword(keyword){
        let keywords = this.getKeywords() || [];
        if(keywords.indexOf(keyword) > -1){
           keywords = keywords.filter(item => item != keyword);
        }else{
            if(keywords.length > 5){
                keywords.pop();
            }
        }
        keywords.unshift(keyword);
       this.setItem("keywords",keywords)
    }
   getKeywords(){
       return this.getItem("keywords");
   }
}


export default new Session();